/*
* All code licensed under the GPLv2 and CDDL.
* Please visit http://www.gnu.org/licenses/gpl-2.0.html
* and http://www.sun.com/cddl/cddl.html for the full versions of
* these licenses.
 */

package twitterapp;

/**
 *
 * @author Xolani
 *
 * This class parses the XML data downloaded from the server and converts it to
 * usabale information.
 *
 * This class will generally handle HTTP GET requests, where the client will
 * download data from the server for use locally, while the xmlgeneration class
 * will handle HTTP POST requests, to commit something to the server.
 * 
 */
public class xmlparsing {

    /**
     * Glossary for variables in the OutputArray array:
     * 
     * [0] : User ID. NOTE: This variable can also declare that the XML was an
     *                        invalid version, or was otherwise unable to parse.
     * [1]  : Name.
     * [2]  : Screen name.
     * [3]  : Location.
     * [4]  : Description.
     * [5]  : Profile image URL.
     * [6]  : URL.
     * [7]  : Protected.
     * [8]  : Followers count.
     * [9]  : Profile background color.
     * [10] : Profile text color.
     * [11] : Profile link color.
     * [12] : Profile sidebar fill color.
     * [13] : Profile sidebar border color.
     * [14] : Friends count.
     * [15] : Created at.
     * [16] : Favorites count.
     * [17] : UTC offset.
     * [18] : Timezone.
     * [19] : Profile background image URL.
     * [20] : Profile background title.
     * [21] : Statuses count.
     * [22] : Notifications.
     * [23] : Following?
     * [24] : Latest status.
     *
     * NOTE:
     * This applies ONLY to the VerifyCredentials method.
    */
    public String[] VerifyCredentials (String xmlcode)
    {
        String[] OutputArray = new String[25];
        int checkxml = xmlcheck (xmlcode);
        if (checkxml == 1)
        {
            xmlcode.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "");
            OutputArray[0] = XmlParseUserId(xmlcode);
            return OutputArray;
        }
        else if (checkxml == -1)
        {
            OutputArray[0] = "<xml=invalidversion>";
            return OutputArray;
        }
        else if (checkxml == 0)
        {
            OutputArray[0] = "<xml=invalid>";
            return OutputArray;
        }
        else
        {
            OutputArray[0] = "null";
            return OutputArray;
        }
    }

    /*
     * check == 0  : Not valid XML.
     * check == -1 : Not correct XML version.
     * check == 1  : Valid XML.
     */
    private int xmlcheck (String xmlcode)
    {
        int check = 0;

        if (xmlcode.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                == true)
        {
            check = 1;
        }
        else if (xmlcode.contains("<?xml version=\"1.0\" encoding=\"UTF-16\"?>")
                == true)
        {
            check = -1;
        }
        else
        {
            check = 0;
        }
        return check;
    }

    private String XmlParseUserId (String xmlcode)
    {
        String UserId = "";

        UserId = Splitter (xmlcode, "<id>", "</id>");

        return UserId;
    }

    private String XmlParseName (String xmlcode)
    {
        String Name = "";
        return Name;
    }

    private String XmlParseScreenName (String xmlcode)
    {
        String ScreenName = "";
        return ScreenName;
    }

    private String XmlParseLocation (String xmlcode)
    {
        String Location = "";
        return Location;
    }

    private String XmlParseDescription (String xmlcode)
    {
        String Description = "";
        return Description;
    }

    private String XmlParseProfileImage (String xmlcode)
    {
        String ProfileImageURL = "";
        return ProfileImageURL;
    }

    private String XmlParseURL (String xmlcode)
    {
        String URL = "";
        return URL;
    }

    private boolean XmlParseProtected (String xmlcode)
    {
        boolean Protection = false;
        return Protection;
    }

    private int XmlParseFollowerCount (String xmlcode)
    {
        int followercount = 0;
        return followercount;
    }

    private String XmlParseProfileBackgroundColor (String xmlcode)
    {
        String Color = "";
        return Color;
    }

    private String XmlParseProfileTextColor (String xmlcode)
    {
        String Color = "";
        return Color;
    }

    private String XmlParseProfileLinkColor (String xmlcode)
    {
        String Color = "";
        return Color;
    }

    private String XmlParseSidebarFillColor (String xmlcode)
    {
        String Color = "";
        return Color;
    }

    private String XmlParseSidebarBorderColor (String xmlcode)
    {
        String Color = "";
        return Color;
    }

    private int XmlParseFriendsCount (String xmlcode)
    {
        int FriendsCount = 0;
        return FriendsCount;
    }

    private String XmlParseCreated (String xmlcode)
    {
        //This method finds the created date/time from the XML data sent by the
        //server.
        String CreatedDateTime = "";
        return CreatedDateTime;
    }

    private int XmlParseFavoritesCount (String xmlcode)
    {
        int Favorites = 0;
        return Favorites;
    }

    public String XmlParseUTCOffset (String xmlcode)
    {
        String Offset = "";
        return Offset;
    }

    private String XmlParseTimeZone (String xmlcode)
    {
        String TimeZone = "";
        return TimeZone;
    }

    private String XmlParseBackgroundImageURL (String xmlcode)
    {
        String URL = "";
        return URL;
    }

    private String XmlParseProfileBackgroundTitle (String xmlcode)
    {
        String Title = "";
        return Title;
    }

    public int XmlParseStatusesCount (String xmlcode)
    {
        int StatusesCount = 0;
        return StatusesCount;
    }

    private boolean XmlParseNotifications (String xmlcode)
    {
        boolean Notifications = false;
        return Notifications;
    }

    private boolean XmlParseFollowing (String xmlcode)
    {
        boolean Following = false;
        return Following;
    }

    private String XmlParseStatus (String xmlcode)
    {
        //This method will return all of the XML code for the current status,
        //but will not parse it any further. This will be handled by other
        //methods.

        String Status = "";
        return Status;
    }

    private String Splitter (String xmlcode, String StartPattern,
            String EndPattern)
    {
        String returnpattern = "";
        //Convert the parameters to character arrays...
        char[] StartPatternArray = new char[StartPattern.length()];
        StartPatternArray = StartPattern.toCharArray();
        char[] EndPatternArray = new char[EndPattern.length()];
        EndPatternArray = EndPattern.toCharArray();
        char[] XmlcodeArray = new char[xmlcode.length()];
        XmlcodeArray = xmlcode.toCharArray();
        //Get the length of the arrays...
        int StartPatternLength = StartPatternArray.length;
        int EndPatternLength = EndPatternArray.length;
        int XmlcodeLength = XmlcodeArray.length;
        //Variables holding the length of each array, so the program knows when
        //to stop reading for more characters.
        int PatternRemainingChars = 0;
        boolean patternmatch = false;

        for (int XmlRemainingChars = 0; XmlRemainingChars <= XmlcodeLength;
            XmlRemainingChars++)
        {
            if (XmlcodeArray[XmlRemainingChars] ==
                    StartPatternArray[PatternRemainingChars])
            {
                do
                {
                    if (PatternRemainingChars == StartPatternLength)
                    {
                        patternmatch = true;
                        break;
                        //Break from loop, we've met the conditions.
                    }
                    else if (XmlRemainingChars == XmlcodeLength)
                    {
                        patternmatch = false;
                        break;
                        //Reached the end of the XML code; break.
                    }
                    else if (XmlcodeArray[XmlRemainingChars] ==
                            StartPatternArray[PatternRemainingChars])
                        {patternmatch = true; PatternRemainingChars++;}
                    else
                    {patternmatch = false;}
                }
                while (patternmatch == true);
            }
            else
            {
                patternmatch = false;
                break;
            }
        }
        //To do.
        return returnpattern;
    }
}
